import 'package:flutter/material.dart';
import 'package:flutter_k_chart/chart_style.dart';
import 'package:flutter_k_chart/utils/number_util.dart';

export '../chart_style.dart';

abstract class BaseChartRenderer<T> {
  double maxValue, minValue;
  late double scaleY, scaleX;
  double topPadding;
  Rect chartRect;
  final Paint chartPaint = Paint()
    ..isAntiAlias = true
    ..filterQuality = FilterQuality.high
    ..strokeWidth = 1.0
    ..color = Colors.red;

  Paint get gridPaint {
    final paint = Paint();
    paint
      ..isAntiAlias = true
      ..filterQuality = FilterQuality.high
      ..strokeWidth = 1.0
      ..color = chartColors.gridColor ?? paint.color;
    return paint;
  }

  ChartStyle chartStyle;
  ChartColors chartColors;

  BaseChartRenderer({
    required this.chartRect,
    required this.maxValue,
    required this.minValue,
    required this.topPadding,
    required this.scaleX,
    required this.chartStyle,
    required this.chartColors,
  }) {
    if (maxValue == minValue) {
      maxValue += 0.5;
      minValue -= 0.5;
    }
    scaleY = chartRect.height / (maxValue - minValue);
  }

  double getY(double value) => (maxValue - value) * scaleY + chartRect.top;

  double getPrice(double y) {
    var value = maxValue - (y - chartRect.top) / scaleY;
    return value;
  }

  String format(double n) => NumberUtil.format(n);

  void drawGrid(Canvas canvas, int gridRows, int gridColumns);

  void drawText(Canvas canvas, T data, double x);

  void drawRightText(canvas, textStyle, int gridRows);

  void drawChart(T lastPoint, T curPoint, double lastX, double curX, Size size,
      Canvas canvas);

  void drawLine(double lastPrice, double curPrice, Canvas canvas, double lastX,
      double curX, Color color) {
    double lastY = getY(lastPrice);
    double curY = getY(curPrice);
    canvas.drawLine(
        Offset(lastX, lastY), Offset(curX, curY), chartPaint..color = color);
  }

  TextStyle getTextStyle(Color color, {FontWeight? fontWeight}) {
    return TextStyle(
      fontSize: chartStyle.defaultTextSize,
      color: color,
      fontWeight: fontWeight,
    );
  }
}
